package cn.edu.scau.cmi.dengzhengyu.client;

import cn.edu.scau.cmi.dengzhengyu.safe.SafeComponent;
import cn.edu.scau.cmi.dengzhengyu.safe.SafePerson;
import cn.edu.scau.cmi.dengzhengyu.safe.SafeTeam;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application {
	private Button branchBtn;
	private Button leafBtn;
	private TextField textField;
	private TreeView<SafeComponent> tree;
	private Button deleteBtn;
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		Pane root = new Pane();
		Scene scene = new Scene(root, 600, 550);
		primaryStage.setScene(scene);
		createView(root);
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
	public void createView(Pane pane) {
		// 初始化一棵树作为初始视图
		this.tree = createTreeView(new TreeView<SafeComponent>());
		TreeItem<SafeComponent> root = createComponent(new SafeTeam("大组织"));
		TreeItem<SafeComponent> team1 = createComponent(new SafeTeam("团体1"));
		TreeItem<SafeComponent> team2 = createComponent(new SafeTeam("团体2"));
		TreeItem<SafeComponent> person = createComponent(new SafePerson("小明"));
		this.tree.setRoot(root);
		root.getChildren().add(team1);
		root.getChildren().add(team2);
		team1.getChildren().add(person);
		pane.getChildren().add(this.tree);
		// 初始化控件并绑定监听事件
		this.textField = createInput(pane, "团体/个人名称：", 420.0, 50.0, 300.0, 50.0);
		this.branchBtn = createButton("创建团体",300.0, 130.0);
		this.leafBtn = createButton("添加个人", 400.0, 130.0);
		this.deleteBtn = createButton("删除", 500.0, 130.0);
		createBranch();
		createLeaf();
		deleteNode();
		
		pane.getChildren().addAll(this.textField, this.branchBtn, this.leafBtn, this.deleteBtn);
	}
	
	public void deleteNode() {
		this.deleteBtn.setOnMouseClicked(value -> {
			TreeItem<SafeComponent> item = this.tree.getFocusModel().getFocusedItem().getParent();
			if (item == null) {
				return;
			}
			SafeComponent current = this.tree.getFocusModel().getFocusedItem().getValue();
			SafeComponent parent = item.getValue();
			if (parent != null && parent.getClass().equals(SafeTeam.class)) {
				SafeTeam team = (SafeTeam) parent;
				team.removeChildren(current);
				item.getChildren().remove(this.tree.getFocusModel().getFocusedItem());
			}
		});
	}
	
	public void createBranch() {
		this.branchBtn.setOnMouseClicked(value -> {
			TreeItem<SafeComponent> item = this.tree.getFocusModel().getFocusedItem();
			if (this.textField.getText() != null && this.textField.getText().trim().length() > 0 
					&& item.getValue().getClass().equals(SafeTeam.class)) {
				SafeTeam current = new SafeTeam(this.textField.getText());
				SafeTeam team = (SafeTeam) item.getValue();
				team.addChildren(current);
				item.getChildren().add(createComponent(current));
				this.textField.clear();
			}
		});
	}
	
	public void createLeaf() {
		this.leafBtn.setOnMouseClicked(value -> {
			TreeItem<SafeComponent> item = this.tree.getFocusModel().getFocusedItem();
			if (this.textField.getText() != null && this.textField.getText().trim().length() > 0
					&& item.getValue().getClass().equals(SafeTeam.class)) {
				SafePerson current = new SafePerson(this.textField.getText());
				SafeTeam team = (SafeTeam) item.getValue();
				team.addChildren(current);
				item.getChildren().add(createComponent(current));
				this.textField.clear();
			}
		});
	}
	
	/**
	 * 创建树形结构视图
	 * @param <T>
	 * @param tree
	 * @return
	 */
	public <T> TreeView<T> createTreeView(TreeView<T> tree) {
		tree.setLayoutX(30);
		tree.setLayoutY(50);
		return tree;
	}
	
	/**
	 * 创建安全性组件
	 * @param component
	 * @return
	 */
	public TreeItem<SafeComponent> createComponent(SafeComponent component) {
		TreeItem<SafeComponent> treeItem = new TreeItem<SafeComponent>(component);
		treeItem.setExpanded(true);
		return treeItem;
	}
	
	/**
	 * 创建输入框
	 * @param pane
	 * @param text
	 * @param layoutX
	 * @param layoutY
	 * @param textX
	 * @param textY
	 * @return
	 */
	private TextField createInput(Pane pane, String text, Double layoutX, Double layoutY, Double textX, Double textY) {
		Label label = new Label(text);
		TextField input = new TextField();
		input.setLayoutX(layoutX);
		input.setLayoutY(layoutY);
		label.setLayoutX(textX);
		label.setLayoutY(textY);
		label.setFont(Font.font(13));
		pane.getChildren().add(label);
		return input;
	}
	
	/**
	 * 创建按钮
	 * @param text
	 * @param layoutX
	 * @param layoutY
	 * @return
	 */
	private Button createButton(String text, Double layoutX, Double layoutY) {
		Button button = new Button(text);
		button.setLayoutX(layoutX);
		button.setLayoutY(layoutY);
		return button;
	}
}
